package com.ctshk.task.worker.processors;

import com.ctshk.common.dto.Result;
import com.ctshk.rpc.custom.dto.inquiry.InquiryInfoDTO;
import com.ctshk.rpc.custom.service.ICustomizedInquiryService;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 询价单超时-单机处理器
 * com.ctshk.task.worker.processors.InquiryExpiresProcessor
 *
 * @author 谢诗宏
 * @since 2021-2-1
 */
@Slf4j
@Component
public class InquiryExpiresProcessor implements BasicProcessor {

    @DubboReference
    private ICustomizedInquiryService inquiryService;

    @Override
    public ProcessResult process(TaskContext context) throws Exception {

        OmsLogger omsLogger = context.getOmsLogger();
        List<InquiryInfoDTO> dtos = inquiryService.expiresList();
        Optional.ofNullable(dtos).orElse(new ArrayList<>()).forEach(dto -> {
            Result result = inquiryService.expires(dto.getId());
            if (!result.isSuccess()) {
                omsLogger.error("设置询价单超时失败,询价单id[{}]", dto.getId());
            } else {
                omsLogger.info("设置询价单超时成功,询价单id[{}]", dto.getId());
            }
        });
        // 根据控制台参数判断是否成功
        boolean success = !"failed".equals(context.getJobParams());
        omsLogger.info("InquiryExpiresProcessor finished process,success: .", success);

        return new ProcessResult(success, context + ": " + success);
    }

}
